package com.lanchetech.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.MeetingService;
import com.lanchetech.bean.request.MeetingPageReq;
import com.lanchetech.bean.request.MeetingUserPageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.MeetingUserVO;
import com.lanchetech.bean.vo.MeetingVO;
import com.lanchetech.common.enums.ToggleEnum;
import com.lanchetech.dao.AreaMapper;
import com.lanchetech.dao.IdentityMapper;
import com.lanchetech.dao.MeetingMapper;
import com.lanchetech.dao.MeetingUserMapper;
import com.lanchetech.entity.Meeting;
import com.lanchetech.entity.User;
import com.lanchetech.service.DaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    MeetingMapper meetingMapper;

    @Autowired
    MeetingUserMapper meetingUserMapper;

    @Autowired
    IdentityMapper identityMapper;

    @Autowired
    AreaMapper areaMapper;

    @Autowired
    DaoService daoService;

    @Override
    public ResultData<BasePageResp<MeetingVO>> getMeetingPage(MeetingPageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<MeetingVO> list = meetingMapper.findAllPage(req);
        if (!CollectionUtils.isEmpty(list)) {
            Map<Long, User> userMap = daoService.getUserMap(list.stream().map(MeetingVO::getUserId).collect(Collectors.toList()));
            list.stream().forEach(item -> {
                item.setUserNickname(userMap.get(item.getUserId()).getNickname());
                item.setUserAvatar(userMap.get(item.getUserId()).getAvatar());
            });
        }
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<BasePageResp<MeetingUserVO>> getMeetingUserPage(MeetingUserPageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<MeetingUserVO> list = meetingUserMapper.findAllPage(req);
        if (!CollectionUtils.isEmpty(list)) {
            List<Meeting> meetingList = meetingMapper.findAllByIds(list.stream().map(MeetingUserVO::getMeetingId).collect(Collectors.toList()));
            Map<Long,Meeting> meetingMap = new HashMap<>();
            meetingList.stream().forEach(item->{
                meetingMap.put(item.getId(),item);
            });
            Map<Long, User> userMap = daoService.getUserMap(list.stream().map(MeetingUserVO::getUserId).collect(Collectors.toList()));
            list.stream().forEach(item -> {
                item.setUserNickname(userMap.get(item.getUserId()).getNickname());
                item.setUserAvatar(userMap.get(item.getUserId()).getAvatar());
                item.setMeeting(meetingMap.get(item.getMeetingId()));
            });
        }
        return PageHelpUtil.buildPage(list, page);
    }
}
